package com.seeyon.apps.enxicun.event;

import com.seeyon.apps.collaboration.manager.ColManager;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.cap4.form.modules.event.CAPFormTriggerAfterEvent;
import com.seeyon.cap4.form.modules.event.FormDataAfterSubmitEvent;
import com.seeyon.cap4.form.modules.event.FormDataBeforeSubmitEvent;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.util.annotation.ListenEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname FormDataEvent
 * @Description 底表监听
 * @Date 2022/3/23 11:30
 * @Created by Administrator
 */
public class FormDataEventListener {
    private static Log log = LogFactory.getLog(MemberEventListener.class);// 打印日志
    private static EnumManager enumManager = (EnumManager) AppContext.getBean("enumManagerNew");
    private ColManager colManager = (ColManager) AppContext.getBean("colManager");// AppContext.getBean---自动扫描Spring容器
    private CAP4FormManager cap4FormManager = (CAP4FormManager) AppContext.getBean("cap4FormManager");


    //保存前事件
    @ListenEvent(event = FormDataBeforeSubmitEvent.class)
    public void listenDataBeforeSubmitEvent(FormDataBeforeSubmitEvent event) throws BusinessException {
        log.info("表单触发事件????Before");
        FormDataMasterBean cacheDataBean = (FormDataMasterBean) event.getSource();

        try{
            FormTableBean tableBean = cacheDataBean.getFormTable();
            log.info("tableBean:"+tableBean.toString());
            FormBean formBean = cap4FormManager.getForm(tableBean.getFormId());
            log.info("formBean:"+formBean.toString());
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }

    }


    //保存后事件,
    @ListenEvent(event = FormDataAfterSubmitEvent.class,async = true)
    public void listenDataAfterSubmitEvent(FormDataAfterSubmitEvent event) throws BusinessException {
        log.info("表单触发事件????After");
        log.info("event.getSource()：" + event.getSource());
        FormDataMasterBean cacheDataBean = (FormDataMasterBean) event.getSource();
        //"start_member_id"
        //todo 客户逻辑
        Map<String, Object> tableDataMap = cacheDataBean.getTableDataMap();
        log.info("allDataMap:" + tableDataMap.toString());

        //底表id
        String formId = (String) tableDataMap.get("formId");
        log.info("formId:" + formId);

        //判断是不是表formmain_0027
        if (tableDataMap.get("formmain_0027") != null) {
            log.info("进formmain_0027表");
            HashMap formMainMap = (HashMap) tableDataMap.get("formmain_0027");
            log.info("field0001:" + formMainMap.get("field0001"));
            String label = enumManager.getCtpEnumItem((Long) formMainMap.get("field0016")).getLabel();
            log.info("是否离职:" + label);
            if ("离职".equals(label)) {
                log.info("离职");
            }
            return;
        }
        log.info("不进formmain_0027表");
    }

    //更新底表数据
    @ListenEvent(event = CAPFormTriggerAfterEvent.class)
    public void updateCAPFormEvent(CAPFormTriggerAfterEvent event) throws BusinessException {
        log.info("更新底表数据");
        FormDataMasterBean cacheDataBean = (FormDataMasterBean) event.getSource();
        //todo 客户逻辑
        log.info("cacheDataBean:" + cacheDataBean.getId());
        log.info("getTableDataJSONString:" + cacheDataBean.getTableDataJSONString());

    }
}
